package ro.myApp.client.helpers;

import java.util.ResourceBundle;

import javax.faces.context.FacesContext;

public class ResourceHelperUtils {

	private static final String BUNDLES_CLASS_PATH = "ro.myApp.client.locale.i18n.";
	public static final String DEFAULT_RESOURCE_BUNDLE = "messages";
	public static final String ERRORS_RESOURCE_BUNDLE = "errors";
	
	public static String getMessage(String key, FacesContext context) {
		String text = null;
		try {
			text = getMessage(DEFAULT_RESOURCE_BUNDLE, key, context);
		} catch (Exception e) {
			e.printStackTrace();
			text = "key " + key + " not found";
		}
		return text;

	}

	public static String getMessage(String bundleName, String key, FacesContext context) {
		String text = null;
		try {
			text = getBundle(bundleName, context).getString(key);
		} catch (Exception e) {
			e.printStackTrace();
			text = "key " + key + " not found";
		}
		return text;
	}

	private static ResourceBundle getBundle(String bundleName, FacesContext context) {
		String name = BUNDLES_CLASS_PATH + bundleName;
		ResourceBundle bundle = ResourceBundle.getBundle(name, context.getViewRoot().getLocale());
		return bundle;

	}
}
